package com.ny.demo.demo523;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class LoginSQL {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection conn = Sqlconnect.getCont();
        Scanner  scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();
    //登录的思路：获取用户名和密码，然后去数据库中查询，如果查询到，则登录成功，否则登录失败
        //编写sql语句
        String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
        PreparedStatement state = conn.prepareStatement(sql);
        state.setString(1,username);
        state.setString(2,password);
        ResultSet resultSet = state.executeQuery();
        if (resultSet.next()){
            String username1 = resultSet.getString("username");
            String password1 = resultSet.getString("password");
            System.out.println("用户名"+username1);
            System.out.println("密码"+password1);
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
            conn.close();
            scanner.close();
        }
    }
}
